<template>
  <div>
    <div class="container">
      <div>
        <el-select v-model="value" placeholder="请选择" @change="onChange">
          <el-option key="0" label="0" value="0"></el-option>
          <el-option key="1" label="1" value="1"></el-option>
          <el-option key="2" label="2" value="2"></el-option>
        </el-select>
      </div>
      <div>
        <el-input @input="onKeyWords" placeholder="请输入学生关键字" v-model="name" clearable></el-input>
      </div>
      <div>
        <el-input @input="onPhone" placeholder="请输入手机号" v-model="phone" clearable></el-input>
      </div>
      <div>
        <el-button type="primary" icon="el-icon-search">搜索</el-button>
        <el-button type="info" icon="el-icon-refresh">重置</el-button>
      </div>
    </div>

    <div class="wrapper">
      <el-table class="table" :data="list" style="width: 100%">
        <el-table-column label="日期" width="180">
          <template slot-scope="scope">
            <i class="el-icon-time"></i>
            <span style="margin-left: 10px">{{ scope.row.time }}</span>
          </template>
        </el-table-column>
        <el-table-column label="姓名" width="180">
          <template slot-scope="scope">
            <el-popover trigger="hover" placement="top">
              <p>姓名: {{ scope.row.name }}</p>
              <div slot="reference" class="name-wrapper">
                <el-tag size="medium">{{ scope.row.name }}</el-tag>
              </div>
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column label="性别">
          <template slot-scope="scope">{{ scope.row.sex }}</template>
        </el-table-column>
        <el-table-column label="手机号码">
          <template slot-scope="scope">{{ scope.row.phone }}</template>
        </el-table-column>
        <el-table-column label="状态">
          <template slot-scope="scope">{{ scope.row.state }}</template>
        </el-table-column>
        <el-table-column class="column" label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      name: "",
      phone: "",
      value: "",
      list: [],
      search: []
    };
  },
  mounted() {
    this.$axios.get("http://localhost:8080/list.json").then(res => {
      console.log(res.data);
      this.list = res.data;
      this.search = res.data;
    });
  },
  methods: {
    onChange() {
      // console.log(this.value);
      this.list = [];
      this.search.forEach(ele => {
        // console.log(ele)
        if (ele.state == parseInt(this.value)) {
          this.list.push(ele);
        }
      });
    },
    onKeyWords() {
      this.list = [];
      if (this.name.length != 0) {
        this.search.forEach(ele => {
          if (ele.name.includes(this.name)) {
            this.list.push(ele);
          }
        });
      }
      if (this.name.length == 0) {
        this.list = this.search;
      }
    },
    onPhone() {
      this.list = [];
      if (this.phone.length != 0) {
        this.search.forEach(ele => {
          if (ele.phone.includes(this.phone)) {
            this.list.push(ele);
          }
        });
      }
      if (this.phone.length == 0) {
        this.list = this.search;
      }
    },

    handleEdit(index, row) {
      // console.log(index, row);
    },
    handleDelete(index, row) {
      // console.log(index, row);
    }
  }
};
</script>

<style scoped>
.container {
  width: 100%;
  display: flex;
  justify-content: space-around;
}
.wrapper {
  margin-top: 20px;
}
</style>